package com.campus.lostandfound.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.lostandfound.entity.ClaimRequest;
import com.campus.lostandfound.entity.ItemInfo;
import com.campus.lostandfound.mapper.ClaimRequestMapper;
import com.campus.lostandfound.service.ClaimRequestService;
import com.campus.lostandfound.service.ItemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 认领申请Service实现类
 */
@Service
public class ClaimRequestServiceImpl extends ServiceImpl<ClaimRequestMapper, ClaimRequest> implements ClaimRequestService {
    
    @Autowired
    private ItemInfoService itemInfoService;
    
    @Override
    public boolean submitClaim(ClaimRequest claimRequest) {
        // 生成申请编号
        String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String requestNo = "CR" + dateStr + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
        claimRequest.setRequestNo(requestNo);
        
        // 设置默认状态
        claimRequest.setStatus(0); // 待审核
        
        return save(claimRequest);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean auditClaim(Long id, Integer status, String auditComment) {
        ClaimRequest claimRequest = getById(id);
        if (claimRequest == null) {
            throw new RuntimeException("认领申请不存在");
        }
        
        if (claimRequest.getStatus() != 0) {
            throw new RuntimeException("该申请已审核");
        }
        
        claimRequest.setStatus(status);
        claimRequest.setAuditComment(auditComment);
        claimRequest.setAuditTime(LocalDateTime.now());
        
        // 如果审核通过，更新对应物品信息状态为已认领
        if (status == 1) {
            ItemInfo itemInfo = itemInfoService.getById(claimRequest.getItemInfoId());
            if (itemInfo != null) {
                itemInfo.setStatus(2); // 已认领
                itemInfoService.updateById(itemInfo);
            }
        }
        
        return updateById(claimRequest);
    }
    
    @Override
    public IPage<ClaimRequest> getMyClaims(String userId, Integer current, Integer size) {
        Page<ClaimRequest> page = new Page<>(current, size);
        LambdaQueryWrapper<ClaimRequest> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClaimRequest::getApplicantId, userId);
        wrapper.orderByDesc(ClaimRequest::getCreateTime);
        
        return page(page, wrapper);
    }
    
    @Override
    public IPage<ClaimRequest> getPendingClaims(Integer current, Integer size) {
        Page<ClaimRequest> page = new Page<>(current, size);
        LambdaQueryWrapper<ClaimRequest> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClaimRequest::getStatus, 0);
        wrapper.orderByAsc(ClaimRequest::getCreateTime);
        
        return page(page, wrapper);
    }
}

